package com.kingyea.mobilepolice.redis.service;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2018/4/18 11:45
 */
@Service("redisSyncService")
public class RedisSyncService {

    @Autowired
    private RedisConnectionFactory connectionFactory;// redis链接工厂

    @Autowired
    private RedisTemplate redisTemplate;

    public String test(String val) {
        System.out.println(val);
        System.out.println("redisTemplate test");
        String key = "testKey" + new Date();
        System.out.println(key);
        redisTemplate.opsForValue().set(key, val + new Date());
        return JSON.toJSONString(redisTemplate.opsForValue().get(key));
    }

    public byte[] get(RedisConnection conn, byte[] key) {
        byte[] result = null;
        try {
            if (conn == null) {
                conn = connectionFactory.getConnection();
            }
            result = conn.get(key);
        } catch (Exception e) {
            conn = connectionFactory.getConnection();
            result = conn.get(key);
        } finally {
            conn.close();
            return result;
        }
    }

    public void set(RedisConnection conn, byte[] key, byte[] value) {
        try {
            if (conn == null) {
                conn = connectionFactory.getConnection();
            }
            conn.set(key, value);
        } catch (Exception e) {
            conn = connectionFactory.getConnection();
            conn.set(key, value);
        }
        conn.close();
    }

    public Boolean expire(RedisConnection conn, byte[] key, long seconds) {
        if (seconds < 0) { // token -1 永不过期，不用往下执行了
            return true;
        }
        try {
            if (conn == null) {
                conn = connectionFactory.getConnection();
            }
            return conn.expire(key, seconds);
        } catch (Exception e) {
            conn = connectionFactory.getConnection();
            return conn.expire(key, seconds);
        }
    }

    public Long rPush(RedisConnection conn, byte[] key, byte[]... values) {
        Long result = null;
        try {
            if (conn == null) {
                conn = connectionFactory.getConnection();
            }
            result = conn.rPush(key, values);
        } catch (Exception e) {
            conn = connectionFactory.getConnection();
            result = conn.rPush(key, values);
        } finally {
            conn.close();
            return result;
        }
    }

    public Long del(RedisConnection conn, byte[]... keys) {
        Long result = null;
        try {
            if (conn == null) {
                conn = connectionFactory.getConnection();
            }
            result = conn.del(keys);
        } catch (Exception e) {
            conn = connectionFactory.getConnection();
            result = conn.del(keys);
        } finally {
            conn.close();
            return result;
        }
    }

    public Long lRem(RedisConnection conn, byte[] key, long count, byte[] value) {
        Long result = null;
        try {
            if (conn == null) {
                conn = connectionFactory.getConnection();
            }
            result = conn.lRem(key, count, value);
        } catch (Exception e) {
            conn = connectionFactory.getConnection();
            result = conn.lRem(key, count, value);
        } finally {
            conn.close();
            return result;
        }
    }

    public List<byte[]> lRange(RedisConnection conn, byte[] key, long start, long end) {
        List<byte[]> result = null;
        try {
            if (conn == null) {
                conn = connectionFactory.getConnection();
            }
            result = conn.lRange(key, start, end);
        } catch (Exception e) {
            conn = connectionFactory.getConnection();
            result = conn.lRange(key, start, end);
        } finally {
            conn.close();
            return result;
        }
    }

}
